
/**
 * Module dependencies.
 */

var express = require('express');
var app = express();
var routes = require('./routes');
var authRoutes = require('./routes/users/authentication');
var server = require('http').createServer(app);
var path = require('path');
var mongoose = require('mongoose');
var passport = require('passport');
require('./config/passport')(passport);
var sio = require('socket.io').listen(server);

var MongoStore = require('connect-mongo')(express);

// database connection
mongoose.connect('mongodb://localhost/my9time');


// all environments
app.set('port', process.env.PORT || 8080);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());

app.use(express.cookieParser());
app.use(express.session({
    store: new MongoStore({
        url: 'mongodb://localhost/my9time'
    }),
    secret: '123456789',
    maxAge: new Date(Date.now() + 3600000), // session timeout - for older version
    expires: new Date(Date.now() + 3600000) // session timeout - for later version of express
}));
// passport config
app.use(passport.initialize());
app.use(passport.session());

app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

server.listen(app.get('port'), function(){
    console.log('Express server listening on port ' + app.get('port'));
});

// index
app.get('/', routes.index);
// ====================================================================================
// authentication routes ==============================================================
require('./routes/users/authentication')(app, passport);
require('./routes/notification/notification-socket')(app, sio);

//var notification =
//    sio.of('/notification/').on('connection',function(socket){
//            socket.on('hello',function(data){
//                console.log('Client said to Server: ' + data);
//            });
//            socket.on('helloAll', function(data){
//                socket.broadcast.emit('message',{data: 'Client A just say that: ' + data + ' at '+  new Date.now()});
//            });
//        });

